# -*- coding=utf8 -*-
__author__ = 'nickleo'

import shapeview, topicview, shape_utils, manager_utils
from django.http import HttpResponse, HttpResponseRedirect
import jsonpickle as json
import message_utils as msg
from config import HogWartsConfig as cfg


def get_messages(request):
    result = {"statusCode": 400}
    alert = 'ok'
    # 'type': type,
    # 'namesvr': namesvr,
    # 'typeValue': typeValue,
    # 'key': key,
    # 'id': id
    message =None
    if request.user.is_authenticated and request.user.has_perm('can_del_content_type'):
        type = None
        namesvr = None
        typeValue = None
        key = None
        id = None
        if request.GET.get('type') is None or request.GET.get('type').strip() == '':
            alert = 'type is null'
        else:
            type = request.GET.get('type')
        if request.GET.get('namesvr') is None or request.GET.get('namesvr').strip() == '':
            alert = 'namesvr is null'
        else:
            namesvr = request.GET.get('namesvr')
        if request.GET.get('typeValue') is None or request.GET.get('typeValue').strip() == '':
            alert = 'typeValue is null'
        else:
            typeValue = request.GET.get('typeValue')
        if request.GET.get('key') is None or request.GET.get('key').strip() == '':
            alert = 'key is null'
        else:
            key = request.GET.get('key')

        print type, namesvr, typeValue, key
        if type is not None and namesvr is not None and (
                            typeValue is not None or key is not None ):
            _statusCode, _alert, messages = msg.get_messages(type, namesvr, typeValue, key)
            result["statusCode"] = _statusCode
            result["alert"] = _alert
            result["messages"] = messages
        else:
            alert = "args is null "

    else:
        alert = "User Not perm"

    result["alert"] = alert
    return HttpResponse(json.encode(result))


def get_message_by_id(request):
    alert = 'ok'
    message =None
    statusCode=200
    result={}
    id=None
    if request.user.is_authenticated and request.user.has_perm('can_del_content_type'):
        namesvr = None
        value = None
        if request.GET.get('namesvr') is None or request.GET.get('namesvr').strip() == '':
            alert = 'namesvr is null'
        else:
            namesvr = request.GET.get('namesvr')
        if request.GET.get('id') is None or request.GET.get('id').strip() == '':
            alert = 'id is null'
        else:
            id = request.GET.get('id')

        print namesvr, id
        if namesvr is None:
            namesvr =cfg().get_nameservers()[0]

        print namesvr, id

        if namesvr is not None and id is not None:
            statusCode, alert, message = msg.get_message_by_id(namesvr, id)
        else:
            statusCode = 400
            alert = 'id or namesvr is null'

    result["alert"] = alert
    result["statusCode"] = statusCode
    result["message"] = message
    return HttpResponse(json.encode(result))


